package com.jml;


import java.io.IOException;
import java.util.Properties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;

/**
 * @author:jml
 * @date: 2019/11/14 15:12
 * @description：
 */
public class MyEventPublishingRunListener implements Ordered , SpringApplicationRunListener {
	
	private SpringApplication application;
	private String[] args;
	
	public MyEventPublishingRunListener(SpringApplication application, String[] args) {
		this.application = application;
		this.args = args;
	}
	
	@Override
	public void starting() {
		System.out.println(">>>>starting<<<<<");
	}
	
	//这个方法就是读取我们的配置文件，把文件内容放入容器中
	@Override
	public void environmentPrepared(ConfigurableEnvironment environment) {
		//读取配置文件然后读取到容器中
		Properties properties = new Properties();
		try {
			// 1. 读取我们的my.properties文件
			properties.load(this.getClass().getClassLoader().
					getResourceAsStream("my.properties"));
			// 2. 读取名称名称为my
			PropertySource propertySource = new
					PropertiesPropertySource("my", properties);
			//3. 将资源添加到项目中
			MutablePropertySources propertySources = environment.getPropertySources();
			//把配置文件内容放入容器中
			propertySources.addLast(propertySource);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void contextPrepared(ConfigurableApplicationContext context) {
	
	}
	
	@Override
	public void contextLoaded(ConfigurableApplicationContext context) {
	
	}
	
	@Override
	public void started(ConfigurableApplicationContext context) {
	
	}
	
	@Override
	public void running(ConfigurableApplicationContext context) {
	
	}
	
	@Override
	public void failed(ConfigurableApplicationContext context, Throwable exception) {
	
	}
	
	@Override
	public int getOrder() {
		return -1;
	}
}
